package org.mybatis.generator.plugins.vue.elements;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.xml.TextElement;
import util.IdUtils;

import java.util.List;
import java.util.Random;

/**
 * cjianquan 2019/8/4
 */
public class VueMethodGenerator {
    private IntrospectedTable introspectedTable;

    private String targetModel;

    private String tableName;

    private String tableLab;

    private String code;

    private String sortOrder ;



    public VueMethodGenerator(IntrospectedTable introspectedTable,
                                String targetModel) {
        this.introspectedTable = introspectedTable;
        this.targetModel = targetModel;
        tableName = introspectedTable.getTableConfiguration().getTableName();
        tableName=toLowerCase(tableName);
        tableLab = introspectedTable.getRemarks();
        while (tableName.lastIndexOf("_") != -1) {
            int index = tableName.lastIndexOf("_");
            tableName = tableName.replaceFirst(
                    "_" + tableName.substring(index + 1, index + 2), tableName
                            .substring(index + 1, index + 2).toUpperCase());
        }
        code = targetModel + "_" + tableName;
    }

    public VueMethodGenerator(IntrospectedTable introspectedTable,
                              String targetModel,String sortOrder) {
        this(introspectedTable,targetModel);
        this.sortOrder = sortOrder;
    }

    public TextElement getJsPageMethod(){
        StringBuilder sb = new StringBuilder();
        sb.append("import request from '@/utils/request'\n")
                .append("import axios from 'axios'\n\n")
                .append("export function "+tableName+"List(params) {\n")
                .append("\treturn request({\n")
                .append("\t\turl: '/"+targetModel+"/"+tableName+"/query',\n")
                .append("\t\tmethod: 'get',\n")
                .append("\t\tparams\n")
                .append("\t})\n")
                .append("}\n\n")
                .append("export function "+tableName+"AllList(params) {\n")
                .append("\treturn request({\n")
                .append("\t\turl: '/"+targetModel+"/"+tableName+"/queryByAll',\n")
                .append("\t\tmethod: 'get',\n")
                .append("\t\tparams\n")
                .append("\t})\n")
                .append("}\n\n")
                .append("export function add"+toUpperCase(tableName)+"(params) {\n")
                .append("\treturn request({\n")
                .append("\t\turl: '/"+targetModel+"/"+tableName+"/add',\n")
                .append("\t\tmethod: 'post',\n")
                .append("\t\tparams\n")
                .append("\t})\n")
                .append("}\n\n")
                .append("export function modify"+toUpperCase(tableName)+"(params) {\n")
                .append("\treturn request({\n")
                .append("\t\turl: '/"+targetModel+"/"+tableName+"/modify',\n")
                .append("\t\tmethod: 'post',\n")
                .append("\t\tparams\n")
                .append("\t})\n")
                .append("}\n\n")
                .append("export function delete"+toUpperCase(tableName)+"(params) {\n")
                .append("\treturn request({\n")
                .append("\t\turl: '/"+targetModel+"/"+tableName+"/deleteByBatch',\n")
                .append("\t\tmethod: 'get',\n")
                .append("\t\tparams\n")
                .append("\t})\n")
                .append("}\n\n")
                .append("\n");

        StringBuilder sbMenu = new StringBuilder();
        Long id = IdUtils.id();

        String sqlBefore = "INSERT INTO `VueMenu`(`id`, `path`, `component`, `redirect`, `name`, `title`, `icon`, `parentId`, `status`, `sortNum`, `createTime`, `modifyTime`, `hidden`, `agentId`, `type`) VALUES ";
        sbMenu.append("/**\n")
                .append(sqlBefore)
                .append("("+id+",")
                .append("'/"+tableName+"List',")
                .append("'"+targetModel+"/"+tableName+"List',")
                .append("NULL,")
                .append("'"+tableName+"List',")
                .append("'"+tableLab+"',")
                .append("NULL,")
                .append("10000,1,"+Integer.parseInt(sortOrder)+",")
                .append("now(), now(), NULL, NULL, 3);\n")
                .append(sqlBefore)
                .append("("+IdUtils.id()+",")
                .append("NULL,")
                .append("'"+targetModel+"/"+tableName+"List',")
                .append("NULL,")
                .append("'addRec',")
                .append("'添加',")
                .append("NULL,")
                .append(id+",1,10,")
                .append("now(), now(), NULL, NULL, 2);\n")
                .append(sqlBefore)
                .append("("+IdUtils.id()+",")
                .append("NULL,")
                .append("'"+targetModel+"/"+tableName+"List',")
                .append("NULL,")
                .append("'editRec',")
                .append("'修改',")
                .append("NULL,")
                .append(id+",1,20,")
                .append("now(), now(), NULL, NULL, 2);\n")
                .append(sqlBefore)
                .append("("+IdUtils.id()+",")
                .append("NULL,")
                .append("'"+targetModel+"/"+tableName+"List',")
                .append("NULL,")
                .append("'deleteRec',")
                .append("'删除',")
                .append("NULL,")
                .append(id+",1,30,")
                .append("now(), now(), NULL, NULL, 2);\n\n")
                .append("**/");

        sb.append(sbMenu);

        return new TextElement(sb.toString());
    }

    public TextElement getListPageMethod(){
        StringBuilder sb = new StringBuilder();

        List<IntrospectedColumn> columns = introspectedTable.getBaseColumns();

        StringBuilder sbColumn = new StringBuilder();
        sbColumn.append("\t\t\t\t<el-table-column type=\"selection\" width=\"30\" align=\"center\" ></el-table-column>\n");
        sbColumn.append("\t\t\t\t<el-table-column label=\"序号\" align=\"center\" width=\"50\"  :render-header=\"renderHeader\" >\n")
                .append("\t\t\t\t\t<template slot-scope=\"scope\">{{(params.limit*(params.page-1))+ scope.$index+1 }}</template>\n")
                .append("\t\t\t\t</el-table-column>\n");

        StringBuilder sbIf = new StringBuilder();

        StringBuilder sbSearch = new StringBuilder();
        sbSearch.append("\t\t<el-collapse class=\"page-collapse\">\n")
                .append("\t\t\t<el-collapse-item  name=\"1\"> \n")
                .append("\t\t\t\t<template slot=\"title\"><svg-icon icon-class=\"search\" />&nbsp;条件过滤</template>\n")
                .append("\t\t\t\t<el-form class=\"page-searchbar\" ref=\"form\" :model=\"params\" label-width=\"80px\" size=\"mini\"  @keyup.enter.native=\"search()\">\n")
                .append("\t\t\t\t\t<el-row>\n");
        StringBuilder rangeData = new StringBuilder();
        StringBuilder rangeMethod = new StringBuilder();
        StringBuilder rangeParamsData = new StringBuilder();
        StringBuilder rangeReset = new StringBuilder();
        StringBuilder selList = new StringBuilder();
        for (IntrospectedColumn column : columns) {
            String javaProperty = column.getJavaProperty();
            String remarks = column.getRemarks();
            FullyQualifiedJavaType fqjt = column.getFullyQualifiedJavaType();
            String shortName = fqjt.getShortName();//.equals("Integer")
            sbColumn.append("\t\t\t\t<el-table-column label=\""+remarks+"\" min-width=\"80\" prop=\""+javaProperty+"\" v-if=\"columnIf."+javaProperty+".visible\" align=\"center\" sortable=\"custom\" show-overflow-tooltip >\n");
            sbSearch.append("\t\t\t\t\t\t<el-col :span=\"6\">\n")
                    .append("\t\t\t\t\t\t\t<el-form-item label=\""+remarks+"：\" :label-width=\"formLabelWidth\" >\n");
            if(shortName.equals("Integer") || shortName.equals("Boolean")){
                sbColumn.append("\t\t\t\t\t<template slot-scope=\"scope\">{{ scope.row."+javaProperty+" | "+javaProperty+"Filter }}</template>\n");
                sbSearch.append("\t\t\t\t\t\t\t\t<el-select v-model=\"params."+javaProperty+"\" class=\"filter-item\"  placeholder=\"\" clearable style=\"width: 100%;\">\n")
                        .append("\t\t\t\t\t\t\t\t\t<el-option v-for=\"item in "+javaProperty+"List\" :key=\"item.value\" :label=\"item.label\" :value=\"item.value\"/>\n")
                        .append("\t\t\t\t\t\t\t\t</el-select>\n");
                selList.append("\t\t\t"+javaProperty+"List:global_.elSelectConverter(global_."+javaProperty+"),\n");
            }else if(shortName.equals("Date")){
                sbColumn.append("\t\t\t\t\t<template slot-scope=\"scope\">{{ scope.row."+javaProperty+" | formatDateFilter('yyyy-MM-dd') }}</template>\n");
                sbSearch.append("\t\t\t\t\t\t\t\t<el-date-picker style=\"width:100%\" v-model=\""+javaProperty+"Arr\"\n")
                        .append("\t\t\t\t\t\t\t\t\t\t@change=\""+javaProperty+"Change\" type=\"daterange\" \n")
                        .append("\t\t\t\t\t\t\t\t\t\tvalue-format=\"yyyy-MM-dd\" range-separator=\"-\" \n")
                        .append("\t\t\t\t\t\t\t\t\t\tstart-placeholder=\"开始\" end-placeholder=\"结束\" clearable >\n")
                        .append("\t\t\t\t\t\t\t\t</el-date-picker>\n");
                rangeData.append("\t\t\t"+javaProperty+"Arr:[],\n");
                rangeMethod.append("\t\t"+javaProperty+"Change(){\n")
                        .append("\t\t\tthis.params."+javaProperty+"Start = this."+javaProperty+"Arr[0];\n")
                        .append("\t\t\tthis.params."+javaProperty+"End = this."+javaProperty+"Arr[1];\n")
                        .append("\t\t},\n");
                rangeParamsData.append("\t\t\t\t"+javaProperty+"Start:null,\n")
                        .append("\t\t\t\t"+javaProperty+"End:null,\n");
                rangeReset.append("\t\t\tthis."+javaProperty+"Arr=[];\n");
            }else{
                sbColumn.append("\t\t\t\t\t<template slot-scope=\"scope\">{{ scope.row."+javaProperty+" }}</template>\n");
                sbSearch.append("\t\t\t\t\t\t\t\t<el-input v-model=\"params."+javaProperty+"\" placeholder=\"\" clearable></el-input>\t\n");
            }
            sbColumn.append("\t\t\t\t</el-table-column>\n");

            sbIf.append("\t\t\t\t"+javaProperty+":{ visible:true, label:'"+remarks+"' },\n");

            sbSearch.append("\t\t\t\t\t\t\t</el-form-item>\n");
            sbSearch.append("\t\t\t\t\t\t</el-col>\n");

        }
        sbSearch.append("\t\t\t\t\t\t<el-col :span=\"6\" style=\"float: right;\">\n")
                .append("\t\t\t\t\t\t\t<div class=\"sch_button\" >\n")
                .append("\t\t\t\t\t\t\t\t<el-button type=\"success\"  size=\"mini\"  @click=\"search()\">查询</el-button>\n")
                .append("\t\t\t\t\t\t\t\t<el-button  size=\"mini\"  @click=\"resetSearch()\">清空</el-button>\n")
                .append("\t\t\t\t\t\t\t</div>\n")
                .append("\t\t\t\t\t\t</el-col>\n")
                .append("\n")
                .append("\t\t\t\t\t</el-row>\n")
                .append("\t\t\t\t</el-form>\n")
                .append("\t\t\t</el-collapse-item>\n")
                .append("\t\t</el-collapse>\n")
                .append("\n");


        StringBuilder sbBtn = new StringBuilder();
        sbBtn.append("\t\t<el-row class=\"page-optbutton\">\n")
                .append("\t\t\t<el-button size=\"mini\" type=\"success\" plain @click=\"viewRec\" >查看</el-button>\n")
                .append("\t\t\t<el-button size=\"mini\" type=\"primary\" plain v-bntPerm=\"'addRec'\" @click=\"addRec\" >添加</el-button>\n")
                .append("\t\t\t<el-button size=\"mini\" type=\"warning\" plain v-bntPerm=\"'editRec'\" @click=\"editRec\" >修改</el-button>\n")
                .append("\t\t\t<el-button size=\"mini\" type=\"danger\" plain v-bntPerm=\"'deleteRec'\" @click=\"deleteRec\" >删除</el-button>\n")
                .append("\t\t</el-row>\n")
                .append("\n");

        StringBuilder sbDialog = new StringBuilder();
        sbDialog.append("\t\t<el-dialog v-dialogDrag :title=\"dialogTitle\" v-if=\""+tableName+"ShowDialogVisible\" \n")
                .append("\t\t\t\t:visible.sync=\""+tableName+"ShowDialogVisible\" \n")
                .append("\t\t\t\t:closeOnClickModal=\"false\"  width=\"1300px\" >\n")
                .append("\t\t\t<"+tableName+"ShowDialog :dataObj=\"selObj\" class=\"page-dialogTabDiv\"  ref=\""+tableName+"ShowDialogRef\" @closeDialog=\"closeDialog\"></"+tableName+"ShowDialog>\n")
                .append("\t\t\t<div slot=\"footer\" class=\"dialog-footer\" align=\"center\">\n")
                .append("\t\t\t\t<el-button @click=\""+tableName+"ShowDialogVisible = false\">关闭</el-button>\n")
                .append("\t\t\t</div>\n")
                .append("\t\t</el-dialog>\n")
                .append("\t\t<el-dialog v-dialogDrag :title=\"dialogTitle\"  v-if=\""+tableName+"DialogVisible\" \n")
                .append("\t\t\t\t:visible.sync=\""+tableName+"DialogVisible\" \n")
                .append("\t\t\t\t:closeOnClickModal=\"false\"  width=\"1300px\" >\n")
                .append("\t\t\t<"+tableName+"Dialog :dataObj=\"selObj\" class=\"page-dialogTabDiv\" ref=\""+tableName+"DialogRef\" @closeDialog=\"closeDialog\"></"+tableName+"Dialog>\n")
                .append("\t\t\t<div slot=\"footer\" class=\"dialog-footer\" align=\"center\">\n")
                .append("\t\t\t\t<el-button type=\"primary\" @click=\"$refs."+tableName+"DialogRef.save()\" >保存</el-button>\n")
                .append("\t\t\t\t<el-button @click=\""+tableName+"DialogVisible = false\">关闭</el-button>\n")
                .append("\t\t\t</div>\n")
                .append("\t\t</el-dialog>\n")
                .append("\t\t<el-dialog v-dialogDrag center title=\"自定义列\"  :visible.sync=\"columnDialogVisible\" width=\"800px\" top=\"10vh\" :closeOnClickModal=\"false\">\n")
                .append("\t\t\t<columnDialog :columnIf=\"columnIf\" class=\"page-dialogTabDiv\" ref=\"columnDialogRef\"></columnDialog>\n")
                .append("\t\t\t<div slot=\"footer\" class=\"dialog-footer\" align=\"center\">\n")
                .append("\t\t\t\t<el-button @click=\"columnDialogVisible = false\">关闭</el-button>\n")
                .append("\t\t\t</div>\n")
                .append("\t\t</el-dialog>\n")
                .append("\n");

        sb.append("<template>\n")
                .append("\t<div class=\"page-container\">\n")
                .append(sbSearch)
                .append(sbBtn)
                .append("\t\t<div class=\"page-listtable\">\n")
                .append("\t\t\t<el-table ref=\"dataTableRef\" v-loading=\"listLoading\" \n")
                .append("\t\t\t\t\t:data=\"list\" element-loading-text=\"列表加载中...\"\n")
                .append("\t\t\t\t\tborder fit highlight-current-row \n")
                .append("\t\t\t\t\t@selection-change=\"tableSelectionChange\"\n")
                .append("\t\t\t\t\t@row-click=\"clickRow\" size=\"small\"\n")
                .append("\t\t\t\t\theight=\"660\" @sort-change=\"sortChange\">\n")
                .append(sbColumn)
                .append("\t\t\t</el-table>\n")
                .append("\t\t</div>\n")
                .append("\t\t<div class=\"page-pagination\">\n")
                .append("\t\t\t<el-pagination\n")
                .append("\t\t\t\t:page-sizes=\"$store.getters.page_sizes\"\n")
                .append("\t\t\t\t:page-size=\"params.limit\"\n")
                .append("\t\t\t\tlayout=\"total, sizes, prev, pager, next, jumper,slot\"\n")
                .append("\t\t\t\t:current-page=\"params.page\"\n")
                .append("\t\t\t\t:total=\"total\"\n")
                .append("\t\t\t\t@size-change=\"pageSizeChange\"\n")
                .append("\t\t\t\t@current-change=\"currentPageChange\">\n")
                .append("\t\t\t\t<el-button type=\"text\"  class=\"refreshBnt\" @click=\"refresh()\">\n")
                .append("\t\t\t\t\t<i class=\"el-icon-refresh\">刷新</i>\n")
                .append("\t\t\t\t</el-button>\n")
                .append("\t\t\t</el-pagination>\n")
                .append("\t\t</div>\n")//<div class="page-pagination">  end
                .append(sbDialog)
                .append("\t</div>\n")//<div class="page-container"> end
                .append("</template>\n")
                .append("<script>\n")
                .append("import global_ from \"@/utils/GlobalDict\";\n")
                .append("import {bntPerm,permConfig}  from \"@/utils/btnPermissions.js\";\n")
                .append("import { "+tableName+"List,delete"+toUpperCase(tableName)+" } from \"@/api/"+targetModel+"/"+tableName+"\";\n")
                .append("import columnDialog from '@/views/layout/columnDialog.vue';\n")
                .append("import "+tableName+"ShowDialog from '@/views/"+targetModel+"/dialog/"+tableName+"ShowDialog.vue';\n")
                .append("import "+tableName+"Dialog from '@/views/"+targetModel+"/dialog/"+tableName+"Dialog.vue';\n")
                .append("export default {\n")
                .append("\tcomponents: {\n")
                .append("\t\tcolumnDialog,\n")
                .append("\t\t"+tableName+"ShowDialog,\n")
                .append("\t\t"+tableName+"Dialog,\n")
                .append("\t},\n")
                .append("\tfilters: {\n")
                .append("\t},\n")
                .append("\tdata(){\n")
                .append("\t\treturn {\n")
                .append(selList)
                .append("\t\t\tcolumnIf:{\n")
                .append(sbIf)
                .append("\t\t\t},\n")
                .append("\t\t\tformLabelWidth:'130px',\n")
                .append("\t\t\tparams:{\n")
                .append("\t\t\t\tlimit:10,\n")
                .append("\t\t\t\tpage:1,\n")
                .append(rangeParamsData)
                .append("\t\t\t},\n")
                .append("\t\t\tlistLoading:true,\n")
                .append("\t\t\tlist:[],\n")
                .append("\t\t\ttotal:0,\n")
                .append("\t\t\tselArr:[],\n")
                .append("\t\t\tselObj:new Object(),\n")
                .append("\t\t\tdialogTitle:'编辑',\n")
                .append("\t\t\tcolumnDialogVisible:false,\n")
                .append("\t\t\t"+tableName+"ShowDialogVisible:false,\n")
                .append("\t\t\t"+tableName+"DialogVisible:false,\n")
                .append("\t\t\tidArr:[],\n")
                .append(rangeData)
                .append("\t\t}\n")//return {  end
                .append("\t},\n")// data(){ end
                .append("\tmethods: {\n")
                .append(rangeMethod)
                .append("\t\tshowColumn(event){\n")
                .append("\t\t\tthis.columnDialogVisible = true;\n")
                .append("\t\t},\n")
                .append("\t\trenderHeader(h,{column}){\n")
                .append("\t\t\treturn h(\n")
                .append("\t\t\t\t'span',[\n")
                .append("\t\t\t\th('span', column.label),\n")
                .append("\t\t\t\th('i', {\n")
                .append("\t\t\t\t\tclass:'el-icon-setting',\n")
                .append("\t\t\t\t\tstyle:'color:#409eff;margin-left:5px;',\n")
                .append("\t\t\t\t\ton:{\n")
                .append("\t\t\t\t\t\tclick:this.showColumn\n")
                .append("\t\t\t\t\t}\n")
                .append("\t\t\t\t}\n")
                .append("\t\t\t\t)\n")
                .append("\t\t\t\t],\n")
                .append("\t\t\t);\n")
                .append("\t\t},\n")
                .append("\t\tsortChange({ column, prop, order }){\n")
                .append("\t\t\tif(column){\n")
                .append("\t\t\t\tvar sortText = column.property;\n")
                .append("\t\t\t\tif(order==\"ascending\"){\n")
                .append("\t\t\t\t\tsortText+=\" asc\";\n")
                .append("\t\t\t\t}else{\n")
                .append("\t\t\t\t\tsortText+=\" desc\";\n")
                .append("\t\t\t\t}\n")
                .append("\t\t\t\tthis.params.orderByClause=sortText+\"\";\n")
                .append("\t\t\t\tthis.params.page=1;\n")
                .append("\t\t\t\tthis.refresh();\n")
                .append("\t\t\t}\n")
                .append("\t\t},\n")
                .append("\t\trefresh(){\n")
                .append("\t\t\tthis.listLoading = true;\n")
                .append("\t\t\tthis.getTableList();\n")
                .append("\t\t},\n")
                .append("\t\tgetTableList(){\n")
                .append("\t\t\t"+tableName+"List(this.params).then(response => {\n")
                .append("\t\t\t\tthis.list=response.data.rows;\n")
                .append("\t\t\t\tthis.total= response.data.totalCount;\n")
                .append("\t\t\t\tthis.listLoading=false;\n")
                .append("\t\t\t});\n")
                .append("\t\t\t\n")
                .append("\t\t},\n")
                .append("\t\tpageSizeChange(pageSize){\n")
                .append("\t\t\tthis.params.limit=pageSize;\n")
                .append("\t\t\tthis.refresh();\n")
                .append("\t\t},\n")
                .append("\t\tcurrentPageChange(currentPage){\n")
                .append("\t\t\tthis.params.page=currentPage;\n")
                .append("\t\t\tthis.refresh();\n")
                .append("\t\t},\n")
                .append("\t\tsearch(){\n")
                .append("\t\t\tthis.refresh();\n")
                .append("\t\t},\n")
                .append("\t\tresetSearch(){\n")
                .append("\t\t\tthis.params={};\n")
                .append(rangeReset)
                .append("\t\t\tthis.params.page=1;\n")
                .append("\t\t\tthis.params.limit=10;\n")
                .append("\t\t\tthis.refresh();\n")
                .append("\t\t},\n")
                .append("\t\ttableSelectionChange(s){\n")
                .append("\t\t\tthis.selArr=[];\n")
                .append("\t\t\tthis.idArr=[];\n")
                .append("\t\t\tfor (var i=0;i<s.length;i++){\n")
                .append("\t\t\t\tthis.selArr.push(s[i]);\n")
                .append("\t\t\t\tthis.idArr.push(s[i].id);\n")
                .append("\t\t\t}\n")
                .append("\t\t},\n")
                .append("\t\tclickRow(row){\n")
                .append("\t\t\tthis.$refs.dataTableRef.clearSelection();\n")
                .append("\t\t\tthis.$refs.dataTableRef.toggleRowSelection(row);\n")
                .append("\t\t},\n")
                .append("\t\tisSelectOne(){\n")
                .append("\t\t\tvar text = \"\";\n")
                .append("\t\t\tif(this.selArr.length==1){\n")
                .append("\t\t\t\treturn this.selArr[0];\n")
                .append("\t\t\t}else if(this.selArr.length==0){\n")
                .append("\t\t\t\ttext = \"请选择一条数据\";\n")
                .append("\t\t\t}else{\n")
                .append("\t\t\t\ttext = \"您已选择多条数据！当前操作只能选择一条数据\";\n")
                .append("\t\t\t}\n")
                .append("\t\t\tthis.$alert(text, '提示', {\n")
                .append("\t\t\t\tconfirmButtonText: '确定',\n")
                .append("\t\t\t\tcallback: action => {}\n")
                .append("\t\t\t});\n")
                .append("\t\t\treturn null;\n")
                .append("\t\t},\n")
                .append("\t\tcloseDialog(reload){\n")
                .append("\t\t\tif(reload==1){\n")
                .append("\t\t\t\tthis.getTableList();\n")
                .append("\t\t\t}\n")
                .append("\t\t\tthis.columnDialogVisible=false;\n")
                .append("\t\t\tthis."+tableName+"ShowDialogVisible=false;\n")
                .append("\t\t\tthis."+tableName+"DialogVisible=false;\n")
                .append("\t\t},\n")

                .append("\t\tviewRec(){\n")//查看
                .append("\t\t\tvar obj = this.isSelectOne();\n")
                .append("\t\t\tif(obj!=null){\n")
                .append("\t\t\t\tthis.dialogTitle=\"查看\";\n")
                .append("\t\t\t\tthis.selObj=obj;\n")
                .append("\t\t\t\tthis."+tableName+"ShowDialogVisible=true;\n")
                .append("\t\t\t}\n")
                .append("\t\t},\n")

                .append("\t\taddRec(){\n")//添加
                .append("\t\t\tthis.selObj={};\n")
                .append("\t\t\tthis.dialogTitle=\"添加\";\n")
                .append("\t\t\tthis."+tableName+"DialogVisible=true;\n")
                .append("\t\t},\n")

                .append("\t\teditRec(){\n")//修改
                .append("\t\t\tvar obj = this.isSelectOne();\n")
                .append("\t\t\tif(obj!=null){\n")
                .append("\t\t\t\tthis.dialogTitle=\"修改\";\n")
                .append("\t\t\t\tthis.selObj=Object.assign({},obj);\n")
                .append("\t\t\t\tthis."+tableName+"DialogVisible=true;\n")
                .append("\t\t\t}\n")
                .append("\t\t},\n")

                .append("\t\tdeleteRec(){\n")//删除
                .append("\t\t\tif(this.idArr.length==0){\n")
                .append("\t\t\t\tthis.$alert('请至少选择一条记录', '提示', {\n")
                .append("\t\t\t\t\tconfirmButtonText: '确定',\n")
                .append("\t\t\t\t\tcallback: action => {}\n")
                .append("\t\t\t\t});\n")
                .append("\t\t\t\treturn;\n")
                .append("\t\t\t}\n")
                .append("\t\t\tthis.$confirm('是否删除选中的'+this.idArr.length+'条记录?', '删除', {\n")
                .append("\t\t\t\tconfirmButtonText: '删除',\n")
                .append("\t\t\t\tcancelButtonText: '取消',\n")
                .append("\t\t\t\ttype: 'warning'\n")
                .append("\t\t\t}).then(() => {\n")
                .append("\t\t\t\tvar paraObj = new Object();\n")
                .append("\t\t\t\tparaObj.ids=this.idArr.join(\",\");\n")
                .append("\t\t\t\tdelete"+toUpperCase(tableName)+"(paraObj).then(response => {\n")
                .append("\t\t\t\t\tif(response.data.success){\n")
                .append("\t\t\t\t\t\tthis.$alert(\"删除成功\", \"删除成功\", {\n")
                .append("\t\t\t\t\t\t\tconfirmButtonText: '确定',\n")
                .append("\t\t\t\t\t\t});\n")
                .append("\t\t\t\t\t\tthis.refresh();\n")
                .append("\t\t\t\t\t}else{\n")
                .append("\t\t\t\t\t\tthis.$alert(\"删除失败\", \"删除失败\", {\n")
                .append("\t\t\t\t\t\t\tconfirmButtonText: '确定',\n")
                .append("\t\t\t\t\t\t});\n")
                .append("\t\t\t\t\t}\n")
                .append("\t\t\t\t});\n")
                .append("\t\t\t});\n")
                .append("\t\t},\n")

                .append("\t},\n")//methods: { end
                .append("\tcreated() {\n")
                .append("\t\tpermConfig(this.$route.path);\n")
                .append("\t\tthis.getTableList();\n")
                .append("\t}\n")//created() { end
                .append("}\n")// export default { end
                .append("</script>\n")//<script> end

                .append("\n");


        return new TextElement(sb.toString());
    }

    public TextElement getEditDialogMethod() {
        StringBuilder sb = new StringBuilder();

        sb.append("<template>\n")
                .append("\t<el-form class=\"form\"  :model=\"form\" ref=\"formRef\" :rules=\"rules\" label-width=\"120px\"  size=\"small\">\n")
                .append("\t\t<div class=\"form_box\">\n")
                .append("\t\t\t<el-row>\n");

        List<IntrospectedColumn> columns = introspectedTable.getBaseColumns();
        StringBuilder rules = new StringBuilder();
        StringBuilder filterList = new StringBuilder();
        StringBuilder filterVal = new StringBuilder();
        StringBuilder formInit = new StringBuilder();
        for (IntrospectedColumn column : columns) {
            if (!column.getJavaProperty().equals("id")) {
                sb.append("\t\t\t\t<el-col :span=\"12\">\n")
                        .append("\t\t\t\t\t<el-form-item label=\""+column.getRemarks()+":\" prop=\""+column.getJavaProperty()+"\">\n");

                FullyQualifiedJavaType fqjt = column.getFullyQualifiedJavaType();

                if(fqjt.getShortName().equals("Integer") || fqjt.getShortName().equals("Boolean")){
                    sb.append("\t\t\t\t\t\t<el-select v-model=\"form."+column.getJavaProperty()+"\" filterable>\n")
                            .append("\t\t\t\t\t\t\t<el-option v-for=\"item in "+column.getJavaProperty()+"List\" :key=\"item.value\" :label=\"item.label\" :value=\"item.value\">\n")
                            .append("\t\t\t\t\t\t\t</el-option>\n")
                            .append("\t\t\t\t\t\t</el-select>\n");
                    filterList.append("\t\t\t"+column.getJavaProperty()+"List:global_.elSelectConverter(global_."+column.getJavaProperty()+"),\n");
                }else if(fqjt.getShortName().equals("Date")){
                    formInit.append("\t\tthis.form."+column.getJavaProperty()+"=this.form."+column.getJavaProperty()+"?new Date(this.form."+column.getJavaProperty()+"):null;\n");
                    sb.append("\t\t\t\t\t\t<el-date-picker v-model=\"form."+column.getJavaProperty()+"\" type=\"date\" value-format=\"yyyy-MM-dd\"></el-date-picker>\n");
                }else{
                    sb.append("\t\t\t\t\t\t<el-input v-model=\"form."+column.getJavaProperty()+"\" placeholder=\"请填写"+column.getRemarks()+"\" ></el-input>\n");
                }
                rules.append("\t\t\t\t"+column.getJavaProperty()+":[{required: true, message: '请填写"+column.getRemarks()+"', trigger: 'blur'},],\n");


                sb.append("\t\t\t\t\t</el-form-item>\n")
                        .append("\t\t\t\t</el-col>\n");
            }
        }

        sb.append("\t\t\t</el-row>\n")
                .append("\t\t</div>\n")
                .append("\t</el-form>\n")
                .append("</template>\n");

        sb.append("<script>\n");
        sb.append("import global_ from \"@/utils/GlobalDict\";\n");
        sb.append("import { modify"+toUpperCase(tableName)+",add"+toUpperCase(tableName)+" } from \"@/api/"+targetModel+"/"+tableName+"\";\n");
        sb.append("export default {\n")
                .append("\tprops: {\n")
                .append("\t\tdataObj: {\n")
                .append("\t\t\ttype: Object,\n")
                .append("\t\t\tdefault: new Object()\n")
                .append("\t\t},\n")
                .append("\t},\n")
                .append("\tfilters: {\n")
                .append("\t},\n")
                .append("\tdata(){\n")
                .append("\t\treturn {\n")
                .append("\t\t\tform:new Object(),\n")
                .append(filterList)
                .append("\t\t\trules:{\n")
                .append(rules)
                .append("\t\t\t},\n")
                .append("\t\t}\n")//return { 的结束
                .append("\t},\n")//data(){ 的结束
                .append("\tmethods:{\n")
                .append("\t\tsave(){\n")
                .append("\t\t\tthis.$refs['formRef'].validate((valid) => {\n")
                .append("\t\t\t\tif(valid) {\n")
                .append("\t\t\t\t\tthis.doSave();\n")
                .append("\t\t\t\t}else{\n")
                .append("\t\t\t\t\treturn null;\n")
                .append("\t\t\t\t}\n")
                .append("\t\t\t});\n")
                .append("\t\t},\n")
                .append("\t\tdoSave(){\n")
                .append("\t\t\tif(this.form.id){\n")
                .append("\t\t\t\tmodify"+toUpperCase(tableName)+"(this.form).then(response => {\n")
                .append("\t\t\t\t\tthis.saveCallBack(response);\n")
                .append("\t\t\t\t});\n")
                .append("\t\t\t}else{\n")
                .append("\t\t\t\tadd"+toUpperCase(tableName)+"(this.form).then(response => {\n")
                .append("\t\t\t\t\tthis.saveCallBack(response);\n")
                .append("\t\t\t\t});\n")
                .append("\t\t\t}\n")
                .append("\t\t},\n")
                .append("\t\tsaveCallBack(response){\n")
                .append("\t\t\tif(response.data.success){\n")
                .append("\t\t\t\tthis.$notify({\n")
                .append("\t\t\t\t\ttitle: '提示',\n")
                .append("\t\t\t\t\tmessage: '操作成功',\n")
                .append("\t\t\t\t\ttype: 'success',\n")
                .append("\t\t\t\t\tduration: 2000\n")
                .append("\t\t\t\t})\n")
                .append("\t\t\t\tthis.closeDialog(1)\n")
                .append("\t\t\t}else{\n")
                .append("\t\t\t\tthis.$notify({\n")
                .append("\t\t\t\t\ttitle: '提示',\n")
                .append("\t\t\t\t\tmessage: '操作失败',\n")
                .append("\t\t\t\t\ttype: 'danger',\n")
                .append("\t\t\t\t\tduration: 2000\n")
                .append("\t\t\t\t})\n")
                .append("\t\t\t}\n")
                .append("\t\t},\n")//saveCallBack 的结束
                .append("\t\tcloseDialog(reload){\n")
                .append("\t\t\tthis.$emit(\"closeDialog\",reload);\n")
                .append("\t\t},\n")
                .append("\t},\n")//methods:{ 的结束
                .append("\tcreated(){\n")
                .append("\t\tthis.form = this.dataObj;\n")
                .append(formInit)
                .append("\t},\n")//created(){的结束
                .append("}\n");//export default { 的结束
        sb.append("</script>\n");

        return new TextElement(sb.toString());
    }



    public TextElement getShowDialogMethod() {
        StringBuilder sb = new StringBuilder();

        sb.append("<template>\n")
//                .append("\t<div class=\"page-container\">\n")
                .append("\t<el-form class=\"form\"  :model=\"form\" ref=\"formRef\"  label-width=\"120px\"  size=\"small\">\n")
                .append("\t\t<div class=\"form_box\">\n")
                .append("\t\t<div class=\"title\">信息</div>\n")
                .append("\t\t\t<el-row>\n");

        List<IntrospectedColumn> columns = introspectedTable.getBaseColumns();
        for (IntrospectedColumn column : columns) {
            if (!column.getJavaProperty().equals("id")) {
                sb.append("\t\t\t\t<el-col :span=\"12\">\n")
                        .append("\t\t\t\t\t<el-form-item label=\""+column.getRemarks()+":\">\n");

                sb.append("\t\t\t\t\t\t{{form."+column.getJavaProperty());
                FullyQualifiedJavaType fqjt = column.getFullyQualifiedJavaType();

                if(fqjt.getShortName().equals("Integer") || fqjt.getShortName().equals("Boolean")){
                    sb.append(" | "+column.getJavaProperty()+"Filter ");
                }else if(fqjt.getShortName().equals("Date")){
                    sb.append(" | formatDateFilter('yyyy-MM-dd hh:mm:ss') ");
                }


                sb.append("}}\n")
                        .append("\t\t\t\t\t</el-form-item>\n")
                        .append("\t\t\t\t</el-col>\n");
            }
        }

        sb.append("\t\t\t</el-row>\n")
                .append("\t\t</div>\n")
                .append("\t</el-form>\n")
          .append("</template>\n");

        sb.append("<script>\n");
//        sb.append("\timport global_ from \"@/utils/GlobalDict\";//引入字典\n");
        sb.append("export default {\n")
                .append("\tprops: {\n")
                .append("\t\tdataObj: {\n")
                .append("\t\t\ttype: Object,\n")
                .append("\t\t\tdefault: new Object()\n")
                .append("\t\t},\n")
                .append("\t},\n")
                .append("\tfilters: {\n")
                .append("\t},\n")
                .append("\tdata(){\n")
                .append("\t\treturn {\n")
                .append("\t\t\tform:new Object(),\n")
                .append("\t\t}\n")//return { 的结束
                .append("\t},\n")//data(){ 的结束
                .append("\tmethods:{\n")
                .append("\t},\n")//methods:{ 的结束
                .append("\tcreated(){\n")
                .append("\t\tthis.form = this.dataObj;\n")
                .append("\t},\n")//created(){的结束
                .append("}\n");//export default { 的结束
        sb.append("</script>\n");

        return new TextElement(sb.toString());
    }


    protected String toLowerCase(String tableName) {
        StringBuilder sb = new StringBuilder(tableName);
        sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
        return sb.toString();
    }

    /**
     * BaseUsers to baseUsers
     *
     * @param tableName
     * @return
     */
    protected String toUpperCase(String tableName) {
        StringBuilder sb = new StringBuilder(tableName);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        return sb.toString();
    }

}
